<template>
  <div class="list-page">
    <Row>
      <div class="toolbar">
        <div v-if="tepee === '1'">
          <router-link :to="{ path: '/agent/add' }">
            <Button
              type="primary"
              icon="md-add"
              v-if="Validation.check(Resources.AGENT_ADD)"
              >新增经销商
            </Button>
          </router-link>
        </div>
        <CompanySelector
          style="width: 300px; margin-right: 10px"
          :tagCount="100"
          :companyId="bloc.form.companyId"
          @on-change="(value) => (bloc.form.companyId = value)"
          v-if="Validation.isRootCompany"
        ></CompanySelector>
        <Input
          placeholder="经销商名称"
          v-model="bloc.form.agentName"
          style="width: 180px; margin-right: 10px"
        />
        <!-- <RoleSelectorSingle
          style="width: 180px; margin-right: 10px"
          :tagCount="1"
          :roleId="Number(bloc.form.roleId)"
          @on-change="(value) => (bloc.form.roleId = value)"
        ></RoleSelectorSingle> -->
        <Input
          placeholder="联系人"
          v-model="bloc.form.linkName"
          style="width: 180px; margin-right: 10px"
        />
        <Input
          placeholder="手机号码"
          v-model="bloc.form.phone"
          style="width: 180px; margin-right: 10px"
        />
        <DatePicker
          type="daterange"
          placeholder="添加时间"
          format="yyyy/MM/dd"
          v-model="bloc.form.date"
          :editable="false"
          style="width: 200px; margin-right: 10px"
        ></DatePicker>
        <Button
          type="primary"
          icon="ios-search"
          style="margin-right: 10px"
          size="default"
          @click="bloc.load(1, bloc.paged.pageSize)"
          >查询
        </Button>
        <Button
          icon="md-refresh"
          style="margin-right: 10px"
          size="default"
          @click="bloc.reset()"
          >重置</Button
        >
      </div>
    </Row>
    <Row>
      <Table
        border
        :loading="bloc.loading"
        :columns="columns"
        highlight-row
        :data="bloc.paged.items"
      >
        <template slot-scope="{ row }" slot="action">
          <div style="white-space: nowrap">
            <Button
              size="small"
              style="margin-left: 5px; margin-right: 5px"
              @click="modifyItem(row)"
              type="info"
              v-if="Validation.check(Resources.AGENT_UPDATE)"
              >编辑
            </Button>
            <Button
              size="small"
              @click="bloc.changePsdModal(row.id)"
              v-if="Validation.check(Resources.AGENT_UPDATE_USER_PASSWORD)"
              type="info"
            >
              修改密码
            </Button>
            <Button
              size="small"
              style="margin-left: 5px; margin-right: 5px"
              @click="removeItem(row)"
              type="warning"
              v-if="Validation.check(Resources.AGENT_DELETE)"
            >
              删除
            </Button>
          </div>
        </template>
        <slot name="footer" slot="footer">
          <Page
            show-elevator
            show-total
            show-sizer
            :total="bloc.paged.itemCount"
            :current="bloc.paged.pageIndex"
            :page-size="bloc.paged.pageSize"
            :page-size-opts="[15, 30, 50, 100]"
            @on-change="(val) => bloc.load(val, bloc.paged.pageSize)"
            @on-page-size-change="(val) => bloc.load(bloc.paged.pageIndex, val)"
          />
        </slot>
      </Table>
    </Row>
    <ChangeAgentPwd
    :showPsdModal="bloc.isShowPsdModal"
    :idx="bloc.agentId"
    @savePwdModal="bloc.savePwdModal()"
    @closeModal="bloc.isShowPsdModal = false"
  ></ChangeAgentPwd>
  </div>
</template>

<script lang="ts">
import { Component, Vue } from 'vue-property-decorator';
import { Route } from 'vue-router';
import { ListBloc } from '@/view/agent/ListBloc';
import { Agent } from '@/base/model/Agent';
import { formatDate } from '@/base/util/Dates';
import CompanySelector from '@/component/CompanySelector.vue';
import ChangeAgentPwd from '@/component/ChangeAgentPwd.vue';
import RoleSelectorSingle from '@/component/RoleSelectorSingle.vue';
import { Validation } from '@/base/Validation';
import { Resources } from '@/base/Resources';
import { store } from '@/store/Store';

const columns = (bloc: ListBloc) => [
  {
    type: 'selection',
    width: 40,
    align: 'left'
  },
  // {
  //   title: '所属品牌商',
  //   render: (h: any, params: any) => {
  //     return h('span', {
  //       domProps: {
  //         innerText: bloc.getCompanyName(params.row.companyId)
  //       }
  //     });
  //   },
  //   align: 'left',
  //   minWidth: 150,
  //   tooltip: true
  // },
  {
    title: '经销商名称',
    key: 'agentName',
    align: 'left',
    minWidth: 150,
    tooltip: true
  },
  {
    title: '经销商账号',
    key: 'agentCode',
    align: 'left',
    minWidth: 150,
    tooltip: true
  },
  {
    title: '地址',
    key: 'address',
    minwidth: 100,
    align: 'left',
    tooltip: true
  },
  {
    title: '联系人',
    key: 'linkName',
    align: 'left',
    width: 100,
    tooltip: true
  },
  {
    title: '手机',
    key: 'phone',
    align: 'left',
    width: 120,
    tooltip: true
  },
  {
    title: '添加时间',
    render: (h: any, params: any) => {
      return h('span', {
        domProps: {
          innerText: formatDate(
            new Date(params.row.created),
            'yyyy-MM-dd hh:mm:ss'
          )
        }
      });
    },
    width: 160,
    align: 'left',
    tooltip: true
  },
  {
    title: '操作',
    slot: 'action',
    width: 200,
    align: 'left',
    fixed: 'right'
  }
];

@Component({ components: { ChangeAgentPwd,
        CompanySelector,
        RoleSelectorSingle } })
export default class extends Vue {
  beforeRouteEnter(to: Route, from: Route, next: (vm: any) => void) {
    next((vm) => {
      vm.bloc
        .load(vm.bloc.paged.pageIndex, vm.bloc.paged.pageSize)
        .catch((e) => {
          vm.$Message.error('操作错误');
        });
    });
  }

  bloc: ListBloc = new ListBloc();
  Validation = Validation;
  Resources = Resources;
  columns = columns(this.bloc);

  removeItem(dataItem: Agent) {
    this.$Modal.confirm({
      title: '提示',
      content: '确认删除后将无法恢复！还要继续吗？',
      okText: '删除',
      cancelText: '取消',
      onOk: () => {
        this.bloc
          .removeItem(dataItem)
          .then((value) => {
            this.bloc.load(this.bloc.paged.pageIndex, this.bloc.paged.pageSize);
          })
          .then(() => {
            this.$Message.success('删除成功');
          })
          .catch((e) => {
            this.$Message.error('删除失败');
          });
      }
    });
  }

  modifyItem(dataItem: Agent) {
    this.$router.replace(`/agent/${dataItem.id}`);
  }
  get tepee() {
    return store.state.user.tepee
  }
}
</script>

<style lang="less">
</style>
